home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / shlex.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  8KB  |  217 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''A lexical analyzer class for simple shell-like syntaxes.'''
  5. import os.path as os
  6. import sys
  7. from collections import deque
  8.  
  9. try:
  10.     from cStringIO import StringIO
  11. except ImportError:
  12.     from StringIO import StringIO
  13.  
  14. __all__ = [
  15.     'shlex',
  16.     'split']
  17.  
  18. class shlex:
  19.     '''A lexical analyzer class for simple shell-like syntaxes.'''
  20.     
  21.     def __init__(self, instream = None, infile = None, posix = False):
  22.         if isinstance(instream, basestring):
  23.             instream = StringIO(instream)
  24.         
  25.         if instream is not None:
  26.             self.instream = instream
  27.             self.infile = infile
  28.         else:
  29.             self.instream = sys.stdin
  30.             self.infile = None
  31.         self.posix = posix
  32.         if posix:
  33.             self.eof = None
  34.         else:
  35.             self.eof = ''
  36.         self.commenters = '#'
  37.         self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  38.         if self.posix:
  39.             self.wordchars += '\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
  40.         
  41.         self.whitespace = ' \t\r\n'
  42.         self.whitespace_split = False
  43.         self.quotes = '\'"'
  44.         self.escape = '\\'
  45.         self.escapedquotes = '"'
  46.         self.state = ' '
  47.         self.pushback = deque()
  48.         self.lineno = 1
  49.         self.debug = 0
  50.         self.token = ''
  51.         self.filestack = deque()
  52.         self.source = None
  53.         if self.debug:
  54.             print 'shlex: reading from %s, line %d' % (self.instream, self.lineno)
  55.         
  56.  
  57.     
  58.     def push_token(self, tok):
  59.         '''Push a token onto the stack popped by the get_token method'''
  60.         if self.debug >= 1:
  61.             print 'shlex: pushing token ' + repr(tok)
  62.         
  63.         self.pushback.appendleft(tok)
  64.  
  65.     
  66.     def push_source(self, newstream, newfile = None):
  67.         """Push an input source onto the lexer's input source stack."""
  68.         if isinstance(newstream, basestring):
  69.             newstream = StringIO(newstream)
  70.         
  71.         self.filestack.appendleft((self.infile, self.instream, self.lineno))
  72.         self.infile = newfile
  73.         self.instream = newstream
  74.         self.lineno = 1
  75.         if self.debug:
  76.             if newfile is not None:
  77.                 print 'shlex: pushing to file %s' % (self.infile,)
  78.             else:
  79.                 print 'shlex: pushing to stream %s' % (self.instream,)
  80.         
  81.  
  82.     
  83.     def pop_source(self):
  84.         '''Pop the input source stack.'''
  85.         self.instream.close()
  86.         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
  87.         if self.debug:
  88.             print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
  89.         
  90.         self.state = ' '
  91.  
  92.     
  93.     def get_token(self):
  94.         """Get a token from the input stream (or from stack if it's nonempty)"""
  95.         if self.pushback:
  96.             tok = self.pushback.popleft()
  97.             if self.debug >= 1:
  98.                 print 'shlex: popping token ' + repr(tok)
  99.             
  100.             return tok
  101.         raw = self.read_token()
  102.         while raw == self.eof:
  103.             if not self.filestack:
  104.                 return self.eof
  105.             self.pop_source()
  106.             raw = self.get_token()
  107.             continue
  108.             self.filestack
  109.         return raw
  110.  
  111.     
  112.     def read_token(self):
  113.         quoted = False
  114.         escapedstate = ' '
  115.         while True:
  116.             nextchar = self.instream.read(1)
  117.             if nextchar == '\n':
  118.                 self.lineno = self.lineno + 1
  119.             
  120.             if self.debug >= 3:
  121.                 print 'shlex: in state', repr(self.state), 'I see character:', repr(nextchar)
  122.             
  123.             None if self.state is None else nextchar in self.escape
  124.             if self.state in self.quotes:
  125.                 quoted = True
  126.                 if not nextchar:
  127.                     if self.debug >= 2:
  128.                         print 'shlex: I see EOF in quotes state'
  129.                     
  130.                     raise ValueError, 'No closing quotation'
  131.                 nextchar
  132.                 if nextchar == self.state:
  133.                     if not self.posix:
  134.                         self.token = self.token + nextchar
  135.                         self.state = ' '
  136.                         break
  137.                     else:
  138.                         self.state = 'a'
  139.                 elif self.posix and nextchar in self.escape and self.state in self.escapedquotes:
  140.                     escapedstate = self.state
  141.                     self.state = nextchar
  142.                 else:
  143.                     self.token = self.token + nextchar
  144.             self.state in self.escapedquotes
  145.             None if self.state in self.escape else self.whitespace_split
  146.         result = self.token
  147.         self.token = ''
  148.         if self.posix and not quoted and result == '':
  149.             result = None
  150.         
  151.         if self.debug > 1:
  152.             if result:
  153.                 print 'shlex: raw token=' + repr(result)
  154.             else:
  155.                 print 'shlex: raw token=EOF'
  156.         
  157.         return result
  158.  
  159.     
  160.     def sourcehook(self, newfile):
  161.         '''Hook called on a filename to be sourced.'''
  162.         if newfile[0] == '"':
  163.             newfile = newfile[1:-1]
  164.         
  165.         if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
  166.             newfile = os.path.join(os.path.dirname(self.infile), newfile)
  167.         
  168.         return (newfile, open(newfile, 'r'))
  169.  
  170.     
  171.     def error_leader(self, infile = None, lineno = None):
  172.         '''Emit a C-compiler-like, Emacs-friendly error-message leader.'''
  173.         if infile is None:
  174.             infile = self.infile
  175.         
  176.         if lineno is None:
  177.             lineno = self.lineno
  178.         
  179.         return '"%s", line %d: ' % (infile, lineno)
  180.  
  181.     
  182.     def __iter__(self):
  183.         return self
  184.  
  185.     
  186.     def next(self):
  187.         token = self.get_token()
  188.         if token == self.eof:
  189.             raise StopIteration
  190.         token == self.eof
  191.         return token
  192.  
  193.  
  194.  
  195. def split(s, comments = False, posix = True):
  196.     lex = shlex(s, posix = posix)
  197.     lex.whitespace_split = True
  198.     if not comments:
  199.         lex.commenters = ''
  200.     
  201.     return list(lex)
  202.  
  203. if __name__ == '__main__':
  204.     if len(sys.argv) == 1:
  205.         lexer = shlex()
  206.     else:
  207.         file = sys.argv[1]
  208.         lexer = shlex(open(file), file)
  209.     while None:
  210.         tt = lexer.get_token()
  211.         if tt:
  212.             print 'Token: ' + repr(tt)
  213.             continue
  214.         break
  215.         continue
  216. __name__ == '__main__'
  217.